package groovy.jmx.builder

import groovy.jmx.builder.node.OpNodeData

class JmxOperationFactory extends AbstractFactory {
    OpNodeData data

    public Object newInstance(FactoryBuilderSupport builder, Object nodeName, Object nodeArgs, Map nodeAttribs) {
        // look for at most the name attribute
        if (!nodeAttribs.containsKey("name")) {
            throw new JmxBuilderException("You must provide at least the name of the operation.")
        }
        data = new OpNodeData(
                name: nodeAttribs.remove('name'),
                description: nodeAttribs.remove('description') ?: nodeAttribs.remove('desc'),
                value: nodeAttribs
        )

        if (nodeName.equals("constructor") || nodeName.equals("ctor")) {
            data.constructor = true
        }
        return data
    }

    public boolean onHandleNodeAttributes(FactoryBuilderSupport builder, Object node, Map nodeAttribs) {
        return true;
    }

    public void setParent(FactoryBuilderSupport builder, Object parentNode, Object childNode) {
        if (childNode.constructor)
            parentNode.constructors.add(childNode)
        else
            parentNode.operations.add(childNode)
    }

    public boolean isLeaf() {
        return false
    }
}
